package com.emser.unc.server.restlet;

import java.util.ArrayList;

import org.restlet.resource.ServerResource;

import com.emser.unc.serializable.CustomerDTO;
import com.emser.unc.serializable.CustomerRequestDTO;
import com.emser.unc.serializable.DataStoreVersion;
import com.emser.unc.serializable.SellerCustomersDTO;
import com.emser.unc.serializable.SellerDTO;
import com.emser.unc.server.AppLibCustomer;
import com.emser.unc.server.AppLibSeller;

public class CustomerServerResource extends ServerResource implements CustomerResource{

	@Override
	public SellerCustomersDTO getCustomers(SellerDTO seller) {
		ApplicationRestlet app = (ApplicationRestlet) getApplication();
        if (app.authenticate(getRequest(), getResponse())) {
        	DataStoreVersion version = AppLibSeller.getVersionSeller(seller);
        	SellerCustomersDTO result = new SellerCustomersDTO();
        	Long versionSellerCustomer = seller.getDataSellerversion().getCustomersVersion();
        	if((versionSellerCustomer == null)){
        		ArrayList<CustomerDTO> customersDTO = AppLibCustomer.getSellerCustomers(seller);
            	result.setCustomersDTO(customersDTO);
            	result.setVersion(version.getCustomersVersion());
        	}
        	else if(!version.getCustomersVersion().equals(seller.getDataSellerversion().getCustomersVersion())){
        		ArrayList<CustomerDTO> customersDTO = AppLibCustomer.getSellerCustomers(seller);
            	result.setCustomersDTO(customersDTO);
            	result.setVersion(version.getCustomersVersion());
        	}
        	else
        	{
        		result.setCustomersDTO(new ArrayList<CustomerDTO>());
            	result.setVersion(version.getCustomersVersion());
        	}
        	return result;
        
        }
        else
        	return null;
	}

	@Override
	public Long addCustomer(CustomerRequestDTO customerRequest) {
		ApplicationRestlet app = (ApplicationRestlet) getApplication();
        if (app.authenticate(getRequest(), getResponse())) {
        	Long iDCustomer = AppLibCustomer.addNewCustomer(customerRequest);
        	return iDCustomer;
        }
        else{
        	return null;
        }
		
	}

}
